﻿Imports SIGI.Common.BE

Public Class UC_ABMCommission

#Region "<< -Attributes- >>"
    Private Const _WithoutSelection As String = "- Sin Selección -"

    Private _CommissionController As CommissionCalculationController = Nothing
    Private _CommissionsList As List(Of CommissionCalculationBE) = Nothing
    Private _OperationsList As List(Of OperationBE) = Nothing
    Private _PropertyTypeList As List(Of PropertyTypeBE) = Nothing
#End Region

#Region "<< -Properties- >>"

#End Region

#Region "<< -Events- >>"

    Private Sub UC_ABMCommission_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim wPropertyController As PropertyController = Nothing

        Try
            If Me.DesignMode Then Return

            AddHandler txtMont.KeyPress, AddressOf GenericEvents.TextBoxNumeric_KeyPress
            AddHandler btnNew.Click, AddressOf btnNew_Click
            AddHandler btnUpdate.Click, AddressOf btnUpdate_Click
            AddHandler btnDelete.Click, AddressOf btnDelete_Click
            AddHandler btnCancel.Click, AddressOf btnCancel_Click
            AddHandler btnSave.Click, AddressOf btnSave_Click

            _CommissionController = New CommissionCalculationController

            'Cargo la grilla
            LoadGrid()
            'Busco los tipos de operaciones
            _OperationsList = _CommissionController.SearchOperationTypes()
            _OperationsList.Insert(0, New OperationBE With {.Id = 0, .Description = _WithoutSelection})
            cmbOperationType.ValueMember = "Id"
            cmbOperationType.DisplayMember = "Description"
            cmbOperationType.DataSource = _OperationsList

            'Busco los tipos de propiedad
            wPropertyController = New PropertyController
            _PropertyTypeList = wPropertyController.SearchPropertiesTypes()
            _PropertyTypeList.Insert(0, New PropertyTypeBE With {.Id = 0, .Description = _WithoutSelection})
            cmbPropertyType.ValueMember = "Id"
            cmbPropertyType.DisplayMember = "Description"
            cmbPropertyType.DataSource = _PropertyTypeList

            GroupControl1.Enabled = False
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnNew_Click(sender As System.Object, e As System.EventArgs)
        Try
            ButtonController(btnNew.Name)
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs)
        Try
            ButtonController(btnUpdate.Name)
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs)
        Dim wInputBox As frmInputVisorBox = Nothing
        Dim wCommission As CommissionCalculationBE = Nothing

        Try
            If GridView1.RowCount = 0 Then Return

            If _MsgBox.ShowMessage("ABM Comisiones", "¿Está Seguro de eliminar la Comisión?",
                             frmMessage.MessageType.Question_,
                             frmMessage.MessageButtons.OkCancel) = frmMessage.MsgResult.Cancel Then
                Return
            End If
            wCommission = New CommissionCalculationBE
            wCommission.Operation.Id = cmbOperationType.SelectedValue
            wCommission.PropertyType.Id = cmbPropertyType.SelectedValue

            _CommissionController.Delete(wCommission)

            _MsgBox.ShowMessage("ABM Comisiones", "Datos eliminados con Éxito", frmMessage.MessageType.Information_, frmMessage.MessageButtons.Ok)
            LoadGrid()
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs)
        Dim wCommission As CommissionCalculationBE = Nothing

        Try
            If Not ValidateRequired() Then
                _MsgBox.ShowMessage("ABM Comisiones", "Faltan completar datos obligatorios",
                                    frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
                Return
            End If

            wCommission = New CommissionCalculationBE
            wCommission.Operation.Id = cmbOperationType.SelectedValue
            wCommission.PropertyType.Id = cmbPropertyType.SelectedValue
            wCommission.Mont = Convert.ToDecimal(txtMont.Text)
            wCommission.ValueScale = Convert.ToInt32(IIf(rdPercentage.Checked, CommissionCalculationBE.CommissionType.Percentage,
                                                                CommissionCalculationBE.CommissionType.FixValue))
            If _Behavior = Common.CommonDefinitions.CRUD.Insert Then
                _CommissionController.Insert(wCommission)
                _MsgBox.ShowMessage("ABM Comisiones", "Datos creados con Éxito", frmMessage.MessageType.Information_, frmMessage.MessageButtons.Ok)
            ElseIf _Behavior = Common.CommonDefinitions.CRUD.Update Then
                _CommissionController.Update(wCommission)
                _MsgBox.ShowMessage("ABM Comisiones", "Datos actualizados con Éxito", frmMessage.MessageType.Information_, frmMessage.MessageButtons.Ok)
            End If

            ButtonController(btnSave.Name)

            LoadGrid()
            CleanControls()
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs)
        Try
            ButtonController(btnCancel.Name)
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub

    Private Sub GridView1_RowCellClick(sender As System.Object, e As DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs) Handles GridView1.RowCellClick
        Dim wCommission As CommissionCalculationBE = Nothing

        Try
            If GridView1.RowCount = 0 Then Return

            wCommission = _CommissionsList(e.RowHandle)

            cmbOperationType.SelectedValue = wCommission.Operation.Id
            cmbPropertyType.SelectedValue = wCommission.PropertyType.Id
            txtMont.Text = wCommission.Mont.ToString()
            If wCommission.ValueScale = Convert.ToInt32(CommissionCalculationBE.CommissionType.Percentage) Then
                rdPercentage.Checked = True
            Else
                rdFixMont.Checked = True
            End If
        Catch ex As Exception
            _MsgBox.ShowMessage("Excepción", ex.Message, frmMessage.MessageType.Error_, frmMessage.MessageButtons.Ok)
        End Try
    End Sub
#End Region

#Region "<< -Methods- >>"
    Private Sub LoadGrid()
        _CommissionsList = _CommissionController.Search()
        _CommissionsList.ForEach(Sub(c)
                                     If c.ValueScale = Convert.ToInt32(CommissionCalculationBE.CommissionType.Percentage) Then
                                         c.ValueScaleDescription = GenericMethods.GetEnumItemDescription(CommissionCalculationBE.CommissionType.Percentage)
                                     Else
                                         c.ValueScaleDescription = GenericMethods.GetEnumItemDescription(CommissionCalculationBE.CommissionType.FixValue)
                                     End If
                                 End Sub)

        BindingSource1.DataSource = _CommissionsList
    End Sub

    Private Sub ButtonController(pButton As String)
        Select Case pButton
            Case btnNew.Name
                btnNew.Enabled = False
                btnUpdate.Enabled = False
                btnDelete.Enabled = False
                btnCancel.Enabled = True
                btnSave.Enabled = True
                _Behavior = Common.CommonDefinitions.CRUD.Insert
                GroupControl1.Enabled = True
                CleanControls()

            Case btnUpdate.Name
                btnNew.Enabled = False
                btnUpdate.Enabled = False
                btnDelete.Enabled = False
                btnCancel.Enabled = True
                btnSave.Enabled = True
                _Behavior = Common.CommonDefinitions.CRUD.Update
                GroupControl1.Enabled = True

            Case btnDelete.Name
                _Behavior = Common.CommonDefinitions.CRUD.Delete

            Case btnSave.Name
                btnNew.Enabled = True
                btnUpdate.Enabled = True
                btnDelete.Enabled = True
                btnCancel.Enabled = False
                btnSave.Enabled = False
                _Behavior = Common.CommonDefinitions.CRUD.Read
                GroupControl1.Enabled = False

            Case btnCancel.Name
                btnNew.Enabled = True
                btnUpdate.Enabled = True
                btnDelete.Enabled = True
                btnCancel.Enabled = False
                btnSave.Enabled = False
                _Behavior = Common.CommonDefinitions.CRUD.Read
                GroupControl1.Enabled = False

            Case Else
                _Behavior = Common.CommonDefinitions.CRUD.Read
        End Select
    End Sub

    Private Function ValidateRequired()
        If String.IsNullOrEmpty(txtMont.Text) OrElse Not IsNumeric(txtMont.Text) Then
            Return False
        End If

        If cmbOperationType.SelectedValue = 0 Or cmbPropertyType.SelectedValue = 0 Then
            Return False
        End If

        Return True
    End Function

    Private Sub CleanControls()
        cmbOperationType.SelectedValue = 0
        cmbPropertyType.SelectedValue = 0
        txtMont.Text = String.Empty
        rdPercentage.Checked = True
    End Sub
#End Region

End Class
